package com.java.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Repository;

import com.java.dao.StatusDao;
import com.java.model.PageBean;
import com.java.model.Status;
import com.java.util.DateUtil;
import com.java.util.StringUtil;

/**
 * department
 * 
 * @author ld
 *
 */
@Repository("statusDao")
public class StatusDaoImpl implements StatusDao {

	@Resource
	private JdbcTemplate jdbcTemplate;

	@Override
	public List<Status> find(PageBean pageBean, Status s_status) {
		StringBuffer sb = new StringBuffer("SELECT a.*, c.name, c.phone FROM t_status a LEFT JOIN t_order b ON a.orderId = b.id LEFT JOIN t_company c ON b.companyId = c.id");
		if (s_status != null) {
			if (null != s_status.getOrderId()) {
				sb.append(" and a.orderId = '" + s_status.getOrderId() + "'");
			} else {
				sb.append(" and a.orderId = ''");
			}
		}
		if (pageBean != null) {
			sb.append(" limit " + pageBean.getStart() + "," + pageBean.getPageSize());
		}
		final List<Status> statusList = new ArrayList<Status>();
		System.out.println(sb.toString().replaceFirst("and", "where"));
		jdbcTemplate.query(sb.toString().replaceFirst("and", "where"), new Object[] {}, new RowCallbackHandler() {

			@Override
			public void processRow(ResultSet rs) throws SQLException {
				Status status = new Status();
				status.setId(rs.getInt("id"));
				status.setCreateTime(DateUtil.formatString(rs.getString("createTime"), "yyyy-MM-dd HH:mm:ss"));
				status.setStep(rs.getString("step"));
				status.setOrderId(rs.getInt("orderId"));
				status.setCompanyName(rs.getString("name"));
				status.setCompanyPhone(rs.getString("phone"));
				statusList.add(status);
			}
		});
		return statusList;
	}

	@Override
	public int count(Status s_status) {
		StringBuffer sb = new StringBuffer("select count(*) from t_status");
		if (s_status != null) {
			if (StringUtil.isNotEmpty(s_status.getStep())) {
				sb.append(" and step like '%" + s_status.getStep() + "%'");
			}
		}
		return jdbcTemplate.queryForObject(sb.toString().replaceFirst("and", "where"), Integer.class);
	}

	@Override
	public void add(Status status) {
		String sql = "insert into t_status values(null,now(),?,?)";
		jdbcTemplate.update(sql, new Object[] {status.getStep(), status.getOrderId() });
	}

	@Override
	public Status loadById(int id) {
		String sql = "select * from t_status where id=?";
		final Status status = new Status();
		jdbcTemplate.query(sql, new Object[] { id }, new RowCallbackHandler() {

			@Override
			public void processRow(ResultSet rs) throws SQLException {
				status.setId(rs.getInt("id"));
				status.setCreateTime(rs.getDate("createTime"));
				status.setStep(rs.getString("step"));
				status.setOrderId(rs.getInt("orderId"));
			}
		});
		return status;
	}

	@Override
	public void update(Status status) {
		String sql = "update t_status set step=? where id=?";
		jdbcTemplate.update(sql, new Object[] { status.getStep(), status.getId() });
	}

	@Override
	public void delete(int id) {
		// TODO Auto-generated method stub

	}

	// @Override
	// public void delete(int id) {
	// String sql="delete from t_department where id=?";
	// jdbcTemplate.update(sql, new Object[]{id});
	// }

}
